<template>
  <div class="app-container">
    <!-- 指令管理 -->
    <div class="app-container-search" style="padding-bottom: 10px;">
      <el-form size="small" :model="form" :inline="true" label-width="90px">
        <el-form-item label="设备编号">
          <el-input v-model="form.no" placeholder="请输入" clearable></el-input>
        </el-form-item>
        <el-form-item label="指令状态:">
          <el-select placeholder="请选择" clearable v-model="form.status">
            <el-option label="待下发" value="0"></el-option>
            <el-option label="执行成功" value="1"></el-option>
            <el-option label="执行失败" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属产品">
          <el-select placeholder="请选择" clearable v-model="form.productId">
            <el-option v-for="item in productList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生产厂商">
          <el-select placeholder="请选择" clearable v-model="form.manuId">
            <el-option v-for="item in manufacturerList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>


        <!-- <el-form-item>
          <el-button size="small" type="primary" icon="el-icon-search" @click="getDataList"></el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="small" icon="el-icon-refresh" @click="reset"></el-button>
        </el-form-item> -->
      </el-form>
      <el-button style="margin-left: 20px;" size="small" type="primary" icon="el-icon-search"
        @click="getInstruct">搜索</el-button>
      <el-button size="small" icon="el-icon-refresh">重置</el-button>

    </div>

    <el-divider></el-divider>

    <!-- 表格 -->
    <el-table size="medium" :data="tableData" tooltip-effect="dark" style="width: 100%" border>
      <el-table-column prop="no" label="设备编号" align="center" width="160"></el-table-column>
      <el-table-column prop="productName" label="所属产品" align="center"></el-table-column>
      <el-table-column prop="manuName" label="生产厂商" align="center"></el-table-column>
      <!-- <el-table-column prop="" label="指令编号" align="center"></el-table-column> -->
      <el-table-column prop="code" label="指令内容" align="center" width="280"></el-table-column>
      <el-table-column prop="reason" label="失败原因" align="center"></el-table-column>
      <el-table-column prop="status" label="指令执行状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">待下发</span>
          <span v-if="scope.row.status == 1">执行成功</span>
          <span v-if="scope.row.status == 2">执行失败</span>
        </template>
      </el-table-column>
      <el-table-column prop="handle" label="操作" align="center">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="primary" plain @click="updateCampus(scope.row)">修改</el-button> -->
          <el-button size="mini" type="danger" plain @click="deleteInstruct({ id: scope.row.id })">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination class="pagination" background layout="total, sizes, prev, pager, next, jumper" :total="total"
      :page-size="10" :page-sizes="[10, 20, 30, 40]" @current-change="handleCurrentChange"
      @size-change="handleSizeChange"></el-pagination>





  </div>
</template>

<script>
import { getInstruct, deleteInstruct } from '@/api/instructManage/index.js'
import { getProduct } from '@/api/ProductManage/index.js'
import { getManufacturer } from '@/api/ManufacturerManage/index.js'
export default {
  data() {
    return {
      labelPosition: "left",
      tableData: [],

      form: {
        no: "",
        productId: "",
        manuId: "",
        status: "",
        current: 0,
        field: "",
        order: "",
        pageSize: 10
      },
      total: 0,
      productList: [],
      manufacturerList: []
    }
  },
  methods: {

    //删除指令
    deleteInstruct(e) {
      this.$confirm('此操作将删除该指令, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteInstruct(e).then(res => {
          if (res.data.success == true) {
            this.$message({
              message: '删除成功!',
              duration: 1000,
              type: 'success',
              onClose: () => {
                this.getInstruct()
              }
            })
          } else {
            this.$message({
              message: res.data.message,
              duration: 2000,
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    handleCurrentChange(e) {
      //当前页
      this.form.current = e
      this.getInstruct()
    },
    // 查询指令
    getInstruct() {
      getInstruct(
        this.form
      ).then(res => {
        this.getProduct()
        this.getManufacturer()
        if (res.data.success == true) {

          this.tableData = res.data.data.list
          this.total = res.data.data.total
        } else {
          this.tableData = []
          this.$message({
            message: res.data.message,
            duration: 1000
          })
        }
      })
    },
    // 查询产品
    getProduct() {
      getProduct(
      ).then(res => {
        if (res.data.success == true) {
          this.productList = res.data.data
        }
      })
    },
    // 查询厂商
    getManufacturer() {
      getManufacturer(
      ).then(res => {
        if (res.data.success == true) {
          this.manufacturerList = res.data.data
        }
      })
    }
  },
  created() {
    this.getInstruct()
  },
};
</script>
<style lang="scss" scoped>
.pagination {
  float: right;
  margin-top: 10px;
}
</style>
